import {
    Component,
    OnInit,
    Injector,
    Input,
    Output,
    EventEmitter,
    AfterViewInit,
    ViewEncapsulation,
    ViewChild
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    EditDtoExtensionsItem,
    EditDtoExtensionsType
} from "@shared/service-proxies/stock-service-proxies";
import * as moment from "moment";
import { NgForm } from "@angular/forms";

@Component({
    selector: "dto-extend-edit",
    templateUrl: "./dto-extend.component.html",
    styleUrls: ["./dto-extend.component.less"],
    encapsulation: ViewEncapsulation.None
})
export class DtoExtendEditComponent extends AppComponentBase implements OnInit {
    @ViewChild("extendForm") extendForm: NgForm;

    // SomeOne dto
    _dto: object;
    // SomeOne attrs
    _attrs = new Array<Array<EditDtoExtensionsItem>>();
    // SomeOne Options
    _optionAll: object;

    _w;

    @Input() set optionAll(val) {
        if (val) {
            this._optionAll = val;
        }
    }

    @Input() set dto(val) {
        if (val) {
            this._dto = val;
            Object.keys(val).forEach(function(key) {
                if (moment.isMoment(val[key])) {
                    val[key + "_Date"] = val[key].toDate();
                }
            });
        }
    }
    @Input() set attrs(val: Array<EditDtoExtensionsItem>) {
        if (!val || val.length === 0) {
            return;
        }
        this._attrs = new Array<Array<EditDtoExtensionsItem>>();

        let rowAttrs = new Array<EditDtoExtensionsItem>();
        let rowIdx = 1;

        val.forEach((el, idx) => {
            rowAttrs.push(el);
            if (
                el.type == EditDtoExtensionsType.Select &&
                this._optionAll[el.key + "Options"].length > 0 &&
                (this._dto[el.key] == null || this._dto[el.key] == undefined)
            ) {
                this._dto[el.key] = this._optionAll[
                    el.key + "Options"
                ][0].value;
            }

            if (
                rowIdx % this.col === 0 ||
                (val[idx + 1] && val[idx + 1].type === 8) ||
                val.length === idx + 1
            ) {
                this._attrs.push(rowAttrs);
                rowAttrs = new Array<EditDtoExtensionsItem>();

                rowIdx = 1;
            } else {
                rowIdx++;
            }
        });
    }
    constructor(injector: Injector) {
        super(injector);
    }

    @Input() col = 1;

    @Output() dtoChange: EventEmitter<any> = new EventEmitter();
    ngOnInit(): void {
        this._w = 100 / this.col + "%";
    }

    dateChange(event, key) {
        if (event && moment.isDate(event)) {
            this._dto[key] = moment(event).add("hour", 8);
            this.dtoChange.emit(this._dto);
        }
    }

    converToInt(event) {
        this._dto[event] = parseInt(this._dto[event]);
    }

    converToNumber(event) {
        let t = parseFloat(this._dto[event]);
        if (Number.isNaN(t)) {
            console.log(t);
            this._dto[event] = 0;
        }
    }
}
